<?php

namespace App\Http\Controllers\platform;

use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\CourseClass;
use App\Models\CoursePurchase;
use App\Models\CourseXZ;
use App\Models\ProColumn;
use App\Models\StudyLog;
use App\Models\StudyUserRecord;
use Illuminate\Http\Request;

class CourseController extends Controller
{
    public function index(Request $Request)
    {
        $limit = $Request->limit ?: 15;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $is_have_test = $Request->is_have_test ?? '';
        $first_class = $Request->first_class ?? '';
        $second_class = $Request->second_class ?? '';
        $search_key = $Request->search_key ?? '';
        $grade = $Request->grade ?? '';
        $is_free = $Request->is_free ?? '';

        $sql = CourseXZ::with('videos')->select('id','cover_img','name','uniqid','top_category','secondary_category','chapter','doc_num','question_num','is_off','click_num','purchase_shop','is_complete','grade','sale_price','original_price')
        ->when($is_have_test, function ($query, $is_have_test) {
            if($is_have_test == -1){
                $is_have_test = 0;
            }
            return $query->where('is_have_test', $is_have_test);
        })
        ->when($search_key, function ($query, $search_key) {
            return $query->where('name','like', '%'.$search_key.'%');
        })
        ->when($grade, function ($query, $grade) {
            return $query->where('grade', $grade);
        })
        ->when($is_free, function ($query, $is_free) {
            if($is_free == -1){
                $is_free = 0;
            }
            return $query->where('is_free', $is_free);
        });

        if($Request->is_shop == 1){
            $CoursePurchase = CoursePurchase::where('eid',$Request->eid)->pluck('course_id')->toArray();
            $sql->whereIn('id',$CoursePurchase);
        }
        if($Request->is_shop == -1){
            $CoursePurchase = CoursePurchase::where('eid',$Request->eid)->pluck('course_id')->toArray();
            $sql->whereNotIn('id',$CoursePurchase);
        }

        if($first_class > 0 && !$second_class){
            $second = ProColumn::where('parent_id',$first_class)->pluck('id')->toArray();
            array_push($second,$first_class);
            $class = CourseClass::whereIn('class_id',$second)->pluck('course_id')->toArray();
            $sql->whereIn('id',$class);
        }else if($second_class > 0){
            $class = CourseClass::where('class_id',$second_class)->pluck('course_id')->toArray();
            $sql->whereIn('id',$class);
        }
        
        $sql->orderBy('id','desc');
        if($Request->sort == -1){
            $sql->orderBy('purchase_shop','desc');
        }

        // if($Request->sort == 1){
        //     $sql->orderBy('id','desc');
        // }

        if($Request->sort == 2){
            $sql->where('is_recommend','1');
        }

        if($Request->sort == 3){
            $sql->orderBy('purchase_shop','desc');
        }

        if($Request->sort == 4){
            $sql->orderBy('sale_price','desc');
        }

        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->where('sid',1)->where('is_off',0)->get();
        $shop = Course::where('eid',$Request->eid)->pluck('uniqid')->toArray();
        foreach ($data as $k => $v) {
            $data[$k]->is_shop = 0;
            if(in_array($v->uniqid,$shop)){
                $data[$k]->is_shop = 1;
            }
            $v->chapter = $v->videos->count();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }


    public function study(Request $Request)
    {
        $data = Course::find($Request->id);
        $data->studyVideos;
        $data->comments;
        if($data->studyVideos){
            $study = StudyLog::where('finish',1)->where('task_id',$Request->task_id)->where('uid',$Request->uid)->where('course_id',$Request->id)->get();
            $records = StudyUserRecord::where('course_id',$Request->id)->where('uid',$Request->uid);
            foreach ($data->studyVideos as $k => $v) {
                $is = $records->where('video_id',$v->id)->first();
                $is_study = $study->where('video_id',$v->id)->first();
                $data->studyVideos[$k]->study_time = 0;
                if($is){
                    $data->studyVideos[$k]->study_time = $is->study_length;
                }
                $data->studyVideos[$k]->is_study = 0;
                if(!empty($is_study)){
                    $data->studyVideos[$k]->is_study = 1;
                }
                $data->studyVideos[$k]->progress = round(($data->studyVideos[$k]->study_time / $v->duration) * 100);
            }
        }
        
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function studyXz(Request $Request)
    {
        $data = CourseXz::find($Request->id);
        $data->videos;
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    

    public function syncClass(Request $Request)
    {
        $data = CourseXZ::select('id','secondary_category','top_category')->get();
        $res = [];
        $time = time();
        foreach ($data as $k => $v) {
            $arr = [];
            $arr_ = [];
            $arr['class_id'] = $v->secondary_category;
            $arr['course_id'] = $v->id;
            $arr['create_time'] = $time;
            $arr['update_time'] = $time;
            $arr_['class_id'] = $v->top_category;
            $arr_['course_id'] = $v->id;
            $arr_['create_time'] = $time;
            $arr_['update_time'] = $time;
            $res[] = $arr;
            $res[] = $arr_;
        }
        // CourseClass::insert($res);
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$res]);
    }

}
